package com.yiren.dbaa.module.help_center.presenter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import com.yiren.dbaa.BuildConfig;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.net.oss.IOssFileOperationApi;
import com.yiren.dbaa.net.interf.OssFileCallback;
import com.yiren.dbaa.net.oss.impl.OssFileOperationApiImpl;
import com.yiren.dbaa.module.help_center.bean.ToolkitBean;
import com.yiren.dbaa.module.help_center.contract.ToolkitContract;
import com.yiren.dbaa.module.help_center.model.ToolkitModel;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date :2022/7/28 17:14
 * @description:
 */
public class ToolkitPresenter implements ToolkitContract.Presenter {

    private Activity activity;
    private ToolkitContract.Model toolkitModel;
    private IOssFileOperationApi iOssFileOperationApi;
    private Long totalApk;
    private int sumProgressBarPosition = 0; //进度条累计进度
    private DecimalFormat df = new DecimalFormat("######0.00"); //百分比格式化工具

    public ToolkitPresenter(Activity activity) {
        this.activity = activity;
        toolkitModel = new ToolkitModel(activity);
        iOssFileOperationApi = new OssFileOperationApiImpl(activity);
    }

    @Override
    public void loadToolkits(ToolkitContract.View view) {
         toolkitModel.getToolKit(new CommonCallBack() {
             @Override
             public void success(Object result) {
                 List<ToolkitBean> toolkitBeanList =  (List<ToolkitBean>) result;
                 for (ToolkitBean toolkitBean:toolkitBeanList){
                     toolkitBean.setBtName("下载");
                 }
                 view.showToolkit(toolkitBeanList);
             }

             @Override
             public void fail(String error) {
                 List<ToolkitBean> toolkitBeanList = new ArrayList<>();
                 view.showToolkit(toolkitBeanList);
             }
         });

    }

    @Override
    public void downLoadTookit(ToolkitContract.View view,ToolkitBean toolkitBean) {

       iOssFileOperationApi.downLoadFile(StringConstant.DJ_APK, toolkitBean.getDownloadUrl(), AddressConstant.fetchAvailableSdcardFolder() + "/yiren/dbaf/", StringConstant.APK_NAME, new OssFileCallback() {
           @Override
           public void success(String result) {
               view.downSuccess();
           }

           @Override
           public void contentLength(long total) {
                totalApk = total;
           }

           @Override
           public void progress(int len) {
               sumProgressBarPosition += len;
               activity.runOnUiThread(()->view.showProgress(df.format(((double) sumProgressBarPosition / totalApk) * 100) + "%"));
           }
       });

    }

    @Override
    public void installToolkit(String apkName) {
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        } else {
            intent = new Intent(Intent.ACTION_VIEW);
        }
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        Uri uri;
        File file = new File(AddressConstant.fetchAvailableSdcardFolder() + "/yiren/dbaf/", apkName);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID.concat(".provider"), file);
        } else {
            uri = Uri.fromFile(file);
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        activity.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
